package org.linlinjava.litemall.admin.web;

import io.swagger.annotations.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.admin.service.AdBusinessAuditService;
import org.linlinjava.litemall.admin.service.LogHelper;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.util.YwcgEnumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: Daisen.Z
 * @Date: 2021/6/4 15:40
 * @Version: 1.0
 * @Description:
 */
@RestController
@RequestMapping("/admin/audit")
@Validated
@Api(value = "项目/成果审核状态接口")
public class AdBusinessAuditController {

    @Autowired
    private AdBusinessAuditService auditService;

    private final Log logger = LogFactory.getLog(AdBusinessProjectController.class);

    @Autowired
    private LogHelper logHelper;

    // 提交
    @RequiresPermissions("admin:audit:submit")
    @RequiresPermissionsDesc(menu = {"项目/成果审核状态", "提交"}, button = "提交")
    @GetMapping("/submit")
    public Object submit(String projectId,Integer modelId) {
        Integer status = auditService.selectAuditStatusByPrimaryKey(projectId, modelId);
        if (status < 0){
            return ResponseUtil.fail(999,"项目不存在，请刷新页面后重试");
        }else if (YwcgEnumUtil.StatusAuditCode.waitPass == status){
            return ResponseUtil.fail(999,"项目审核中，请勿重复提交！");
        }else if (YwcgEnumUtil.StatusAuditCode.pass == status){
            return ResponseUtil.fail(999,"审核通过的项目不能再提交！");
        }
        return  auditService.updateStatusAudit(projectId,modelId, YwcgEnumUtil.StatusAuditCode.waitPass,null);
    }


    // 审核
    @RequiresPermissions("admin:audit:pass")
    @RequiresPermissionsDesc(menu = {"项目/成果审核状态", "审核通过"}, button = "审核通过")
    @GetMapping("/pass")
    public Object pass(String projectId,Integer modelId) {
        Integer status = auditService.selectAuditStatusByPrimaryKey(projectId, modelId);
        if (status < 0){
            return ResponseUtil.fail(999,"项目不存在，请刷新页面后重试");
        }else if (YwcgEnumUtil.StatusAuditCode.noSubmit == status || YwcgEnumUtil.StatusAuditCode.backUp == status ){
            return ResponseUtil.fail(999,"项目未提交，请先提交项目！");
        }else if (YwcgEnumUtil.StatusAuditCode.pass == status){
            return ResponseUtil.fail(999,"项目已被审核通过！");
        }
        logHelper.logOrderFail("审核通过","模块ID："+modelId+" 项目ID:"+projectId);
        return  auditService.updateStatusAudit(projectId,modelId, YwcgEnumUtil.StatusAuditCode.pass,null);
    }


    // 退回
    @RequiresPermissions("admin:audit:backup")
    @RequiresPermissionsDesc(menu = {"项目/成果审核状态", "退回"}, button = "退回")
    @GetMapping("/backup")
    public Object backup(String projectId,Integer modelId,String remark) {
        Integer status = auditService.selectAuditStatusByPrimaryKey(projectId, modelId);
        if (status < 0){
            return ResponseUtil.fail(999,"项目不存在，请刷新页面后重试");
        }else if (YwcgEnumUtil.StatusAuditCode.noSubmit == status  || YwcgEnumUtil.StatusAuditCode.backUp == status ){
            return ResponseUtil.fail(999,"项目未提交，请先提交项目！");
        }else if (YwcgEnumUtil.StatusAuditCode.pass == status){
            return ResponseUtil.fail(999,"审核通过的项目不能再退回！");
        }
        return  auditService.updateStatusAudit(projectId,modelId, YwcgEnumUtil.StatusAuditCode.backUp,remark);
    }

}


